import axios from "axios";
import { ElMessage } from "element-plus";
axios.defaults.withCredentials = true

function networkErrorMessage(e) {
  console.error(e)
  ElMessage({
    type: 'error',
    message: "网络请求失败，请检查网络！"
    // i18n.t
  })
}
axios.interceptors.request.use(config => {
  //在发送请求之前做某事
  return config;
},
  error => {
    //请求错误时做些事
    networkErrorMessage(error)
    return Promise.reject(error);
  });
// formData 传参
async function axiosPOSTWithFormData(url: string, params: any, config = null, errMsg = '', successMsg = '') {
  try {
    const res = await axios.post(url, JSON.stringify(params), config || {})
    return resultHandler(res, errMsg, successMsg)
  } catch (e) {
    networkErrorMessage(e)
    return null
  }
}
// body json 传参
async function axiosPOST(url: string, params: any, config = null, errMsg = '', successMsg = '') {
  try {
    const res = await axios.post(url, params, config || {})
    return resultHandler(res, errMsg, successMsg)
  } catch (e) {
    networkErrorMessage(e)
    return null
  }
}

async function axiosPUT(url: string, params: any, config = null, errMsg = '', successMsg = '') {
  try {
    const res = await axios.put(url, params, config || {})
    return resultHandler(res, errMsg, successMsg)
  } catch (e) {
    networkErrorMessage(e)
    return null
  }
}

// config 暂时保留 无用到
async function axiosDELETE(url: string, params: any, config: any = null, errMsg = '', successMsg = '') {
  try {
    let conf: any = {}
    if (config != null) {
      conf = {
        ...config
      }
    }
    conf.params = params
    const res = await axios.delete(url, conf)
    return resultHandler(res, errMsg, successMsg)
  } catch (e) {
    networkErrorMessage(e)
    return null
  }
}


// eslint-disable-next-line no-unused-vars
async function axiosGET(url: string, params: any, config: any = null, errMsg = '', successMsg = '', responseType = null, cache = true) {
  let sendData: any = {
    url: url,
    params: params,
    cache: cache
  }
  if (responseType) {
    sendData.responseType = responseType
  }
  try {
    const res = await axios(sendData)
    return resultHandler(res, errMsg, successMsg)
  } catch (e) {
    networkErrorMessage(e)
    return null
  }
}

function resultHandler(res: any, errMsg: string, successMsg: string) {

  if (res.data.code === 200) {
    if (successMsg != null && successMsg !== '') {
      ElMessage({
        type: 'success',
        message: successMsg,
        duration: 1500
      })
    }
    return res.data.data ? res.data.data : true
  } else if (res.data.code === 10005) {
    ElMessage({
      type: 'error',
      message: '长时间未登录,即将跳转登陆页',
      duration: 2000
    })
    return false
  } else if (res.data.code === 1023) {
    ElMessage({
      type: 'error',
      message: '当前账号在其他地方登陆，请重新登录',
      duration: 2000
    })
    return false
  } else {
    if (errMsg != null) {
      if (errMsg === '') {
        ElMessage({
          type: 'error',
          message: res.data.msg || res.data.message + (typeof res.data.data === 'string' ? res.data.data : '')
        })
      } else {
        ElMessage({
          type: 'error',
          message: errMsg
        })
      }
    }
    return false
  }
}


export {
  axiosGET,
  axiosDELETE,
  axiosPUT,
  axiosPOST,
  axiosPOSTWithFormData
}
